This page last changed on Aug 01, 2005 by aperepel.

You can configure a Spring context for Mule using Mule Xml. This means you can use Spring to wire together a Mule instance without needing to write all the bean definitions yourself, which can be quite time consuming and difficult to maintain. Plus you get the benefits of configuring Mule in domain-specific Mule Xml.

To configure Mule using a Spring context with Mule xml, simply pass the Mule Xml configuration file to the SpringConfigurationBuilder

SpringConfigurationBuilder builder = new SpringConfigurationBuilder();
UMOManager manager = builder.configure("mule-config.xml");

This isn't very useful in itself, but what you can do is combine Spring beans in your Mule Xml to take advantage of Springs advanced wiring capabilities. For example, if you wanted to set a property on a Mule component using a factory method, you can use Spring configuration to do this.

<mule-descriptor name="test" implementation="testObject">
   <properties>
       <bean name="bar" class="org.foo.BarFactory" factory-method="createBar">
            <constructor-arg index="0">
                <value>cocktail</value>
            </constructor-arg>
       </bean>
   </properties>
</mule-descriptor>

There are element name clashes between Spring Xml and Mule Xml configuration. As such certain configuration elements in Spring have to be prepended with spring- when being used in Mule Xml. The following table lists the affected tags.

Spring Element In Mule Xml
<property ...> <spring-property ...>
<map> <spring-map>
<list> <spring-list>
<entry> <spring-entry>

For example, to add a Spring bean property to a Jms Connector -

<connector name="jmsconnector" className"org.mule.providers.jms.JmsConnector">
    <properties>
        <!-- defining a spring property directly -->
        <spring-property name="jndiContext">
            <ref local="myJndicontext"/>
        </spring-property>

        <!-- you can also just nest bean elements -->
        <bean name="connectionFactory" class="org.foo.jms.provider.ConnectionFactory"/>

        <!-- and you can still use normal Mule propery elements -->
        <property name=jndiDestinations" value="true"/>
    </properties>
</connector>

When using spring elements you can use all the configuration elements that Spring provide. The above configures the jndiContext, connectionFactory and jndiDestinations properties on the JmsConnector.

If you want to mix beans in with your Mule Xml (like above) add the following DOCTYPE declaration to your config file

<!DOCTYPE mule-configuration PUBLIC "-//SymphonySoft //DTD mule-configuration XML V1.0//EN"
                      "http://www.symphonysoft.com/dtds/mule/mule-spring-configuration.dtd">

The Spring configuration builder uses the DOCTYPE to determine how to load the configuration file. Any Mule configuration files will be transformed into Spring bean xml before being loaded into a Spring context.

You can mix any number of configuration files when using the SpringConfigurationBuilder just so long as the DOCTYPE declaration uses one of the following dtds.

DTD Type
mule-configuration.dtd Standard Mule xml, no Spring bean definitions
mule-spring-configuration.dtd Mule Xml and Spring Xml combined
spring-beans.dtd Spring beans configuration only

So if you had three different xml configuration files and each one used a different configuration format, defined by the dtds, you could load all of them using the SpringConfigurationBuilder and they can be on the classpath or the file system.

SpringConfigurationBuilder builder = new SpringConfigurationBuilder();
UMOManager manager = builder.configure("spring-beans.xml,mule-configuration.xml,
                                         mule-spring-configuration.xml);
Document generated by Confluence on Nov 27, 2006 10:27